home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WMISC.DOC < prev    next >
Encoding:
Text File  |  1993-02-12  |  11.1 KB  |  346 lines

  1. Miscellaneous library documentation
  2.  
  3. October 11, 1989
  4. February 20, 1991
  5. May 15, 1992 (minor update)
  6. October 20, 1992 (minor update)
  7. January 27, 1993 (minor update)
  8.  
  9. copyright (c) 1992, 1993 by Paul Wheaton
  10.  
  11. Note that this documentation does not cover all of the features of the
  12. provided in this library.  For that, you will have to look at the header files.
  13.  
  14. The Wheaton Libraries are shareware. Anyone may use the libraries provided
  15. that they do not change them.  If you want to change them or if you want
  16. phone support from me, you must pay $50. This in the hope that the hundreds
  17. of vendors that are creating their own string libraries can go with just
  18. one - I'm pretty tired of getting a library in that seems like it will be
  19. pretty cool, just to find out that it uses the same identifiers as
  20. something else and there are then link conflicts.
  21.  
  22. To make these libraries available, you need to include <Misc.h> and link
  23. with WLIB.LIB.
  24.  
  25.       Miscellaneous
  26. 1       Type definitions
  27. 2       Constants
  28. 2.1       Type limits
  29. 2.2       Boolean
  30. 2.3       Position indicator (Left, Right and Center)
  31. 3       Functions
  32. 3.01      Abs()
  33. 3.02      Max()
  34. 3.03      Min()
  35. 3.04      InRange()
  36. 3.05      Delay()
  37. 3.06      Sound()
  38. 3.07      Beep()
  39. 3.08      Randomize()
  40. 3.09      Random()
  41. 3.10      DebugPause()
  42. 3.11      CopyArray()
  43. 3.12      CopyString()
  44. 3.13      ClearStruct()
  45. 3.14      ClearArray()
  46. 3.15      Rounding functions
  47. 3.16      ToUpper() ToLower()
  48. 3.17      PigeonHole()
  49. 3.18      SystemTick()
  50. 3.19      CRC()
  51. 4       Loop handling
  52. 4.1       Loop
  53. 4.2       For
  54.  
  55.  
  56. 1 Type definitions
  57.  
  58.   Using these types allow programmer control over the consistency of what
  59.   they describe from environment to environment.  In K&R C (or pre-ANSI C),
  60.   the definitions where that "int" could be a 32 bit integer on some
  61.   systems and a 16 bit integer on others, whatever seemed more "natural"
  62.   for the computer. Now, the ANSI standard supports the old K&R style but
  63.   have added that although some computers like 32 bit values better, they
  64.   are required to accept the use of 16 bit values.  The compiler may
  65.   actually manipulate the 16 bit value in a 32 bit data area provided that
  66.   the program is treated as though it's a 16 bit area.
  67.  
  68.             Number
  69.   Type      of bits      Sign?
  70.   -------   -------      -----
  71.   Bool          8         No       Use only one bit for True or False
  72.   Byte          8         No
  73.   SByte         8         Yes
  74.   Word         16         No
  75.   SWord        16         Yes
  76.   Long         32         No
  77.   SLong        32         Yes
  78.  
  79. 2 Constants
  80.  
  81. 2.1 Type limits
  82.  
  83.   These limits are for the scalar types defined in section 1.  Of course,
  84.   anything that isn't signed has a minimum of 0.  The max constants are:
  85.   MaxByte, MaxSByte, MaxWord, MaxSWord, MaxLong and MaxSLong.  The min
  86.   constants are:  MinSByte, MinSWord and MinSLong.
  87.  
  88. 2.2 Boolean
  89.  
  90.   Of course, there's True and False.  There's also Yes, No, On and Off.
  91.  
  92. 2.3 Position indicator (Left, Right and Center)
  93.  
  94.   The constants Left, Right and Center are recognized by several
  95.   different functions as parameters.  Display and JustText are two
  96.   that come to mind.  In the case of Display, if you pass Center in
  97.   the X parameter, the String (that is also passed) will be placed
  98.   such that it is centered on that row of the window.  These numbers
  99.   may be stored as large negative values (type SWord), so be careful
  100.   that you don't try to assign them to incompatible types.
  101.  
  102. 3 Functions
  103.  
  104. 3.01 Abs()
  105.  
  106.   Prototype:  [num type] Abs([num type])
  107.  
  108.   Overloaded so that you don't need a different function for every
  109.   type.  Pass any numeric type that will hold a negative number and it
  110.   will return the same type.
  111.  
  112. 3.02 Max()
  113.  
  114.   Prototype:  [num type] Max([num type],[num type])
  115.  
  116.   Overloaded function that takes two arguments of the same type and
  117.   returns the larger value as the same type.
  118.  
  119. 3.03 Min()
  120.  
  121.   Prototype:  [num type] Min([num type],[num type])
  122.  
  123.   Overloaded function that takes two arguments of the same type and
  124.   returns the smaller value as the same type.
  125.  
  126. 3.04 InRange()
  127.  
  128.   Prototype:  Bool InRange([num type] Val, [num type] Low, [num type] High)
  129.  
  130.   Overloaded function that takes three arguments of the same type and
  131.   returns True if the first number is in the range of the other two,
  132.   inclusively.
  133.  
  134. 3.05 Delay()
  135.  
  136.   Prototype:  void Delay(long MilliSecs)
  137.  
  138.   Delay this process for the number of milliseconds passed.  A value
  139.   passed of 1000 means delay for one second.  Note that on
  140.   multi-tasking systems, the operating system will turn the CPU time
  141.   over to something else for this period of time.
  142.  
  143. 3.06 Sound()
  144.  
  145.   Prototype:  void Sound(float Pitch, int MilliSecs)
  146.  
  147.   For values to plug into Pitch, see page 147 of "The Peter Norton
  148.   Programmer's Guide to the IBM PC".  If you don't want to bother to
  149.   look there and don't know what "pitch" or frequency is, then let's
  150.   just say that higher numbers play higher notes.  MilliSecs is how
  151.   long the sound lasts.  This function won't come back until it's done
  152.   making its noise.
  153.  
  154. 3.07 Beep()
  155.  
  156.   Prototype:  void Beep(int Num=1)
  157.  
  158.   For those that are not familiar with default parameters, let me
  159.   point out that you may call this function without any paramters
  160.   (Beep();) and there will be one beep.  You also have the option of
  161.   calling it with one parameter and specify the number of beeps that
  162.   you want.  "Beep(4);" will play 4 beeps.
  163.  
  164. 3.08 Randomize()
  165.  
  166.   Prototype:  void Randomize()
  167.  
  168.   Initializes the random number generator.
  169.  
  170. 3.09 Random()
  171.  
  172.   Prototype:  int Random(int Max)
  173.  
  174.   Returns a random integer in the range of 0..Max-1.
  175.  
  176. 3.10 DebugPause()
  177.  
  178.   Prototype:  void DebugPause(ConstantStringMessage)
  179.  
  180.   Displays the string onto the screen rather sloppily and pauses for a
  181.   second or two.  Since this function uses a macro, the string
  182.   parameter MUST be a string constant.
  183.  
  184.     DebugPause("xxx entering loop");  // valid call
  185.     DebugPause(SomeString_AnyStringType);   // invalid call
  186.  
  187. 3.11 CopyArray()
  188.  
  189.   Prototype:  void* CopyArray(DestArray,SourceArray)
  190.  
  191.   The functions CopyArray, CopyString, ClearStruct and ClearArray all
  192.   expect straight parameters without any extra pointers or
  193.   referencing.  Since these functions are macros, the first (or only)
  194.   parameter must be something that sizeof will work on effectively.
  195.  
  196.   If SourceArray is smaller than DestArray, a bunch of garbage will be
  197.   in the end of DestArray.  If SourceArray is bigger than DestArray,
  198.   only a subset will be copied.  Note that this is just a bitwise copy
  199.   and does not care what the types of either array are.
  200.  
  201.     static int A1[5]={1,2,3,4,5};
  202.     int A2[3];
  203.     CopyArray(A2,A1);  //  A2 now contains the first 3 elements of A1
  204.     int *A3=A2;
  205.     CopyArray(A3,A1);  //  unpredictable results!!!
  206.       // sizeof a pointer probably isn't what is desired
  207.  
  208. 3.12 CopyString()
  209.  
  210.   Prototype: void CopyString(Dest,Source)
  211.  
  212.   Just like CopyArray, cept it will stop if it finds a null character
  213.   and make sure that there's a null character copied over.  Note that
  214.   this is for strings of type char* only!
  215.  
  216. 3.13 ClearStruct()
  217.  
  218.   Prototype: void ClearStruct(S)
  219.  
  220.   Fills the struct with zeros.
  221.  
  222.     struct
  223.       {
  224.         long A,B,C,D,E,F;
  225.       } S;
  226.     ClearStruct(S);
  227.  
  228. 3.14 ClearArray()
  229.  
  230.   Prototype:  ClearArray(A)
  231.  
  232.   Fills the array with zeros.
  233.  
  234.     float F[20];
  235.     ClearArray(F);
  236.  
  237. 3.15 Rounding functions
  238.  
  239.   Prototypes:  long Round(double Val)
  240.                long RoundUp(double Val)
  241.                long RoundDown(double Val)
  242.                long RoundOut(double Val)
  243.                long RoundIn(double Val)
  244.  
  245.   Initial value     .2    .7   -.2   -.7
  246.  
  247.   Round()            0     1     0    -1
  248.   RoundUp()          1     1     0     0
  249.   RoundDown()        0     0    -1    -1
  250.   RoundOut()         1     1    -1    -1
  251.   RoundIn()          0     0     0     0
  252.  
  253. 3.16      ToUpper() ToLower()
  254.  
  255.   Prototypes:  char ToUpper(char C)
  256.                char ToLower(char C)
  257.  
  258.   If a lower case char is passed to ToUpper() it will be converted to
  259.   an upper case char.  Any other characters passed in will be
  260.   unchanged.  ToLower() works the same way except that upper case
  261.   chars are converted to lower case.
  262.  
  263. 3.17      PigeonHole()
  264.  
  265.   Prototype:  void PigeonHole(long TotQuan, int ArrayLen, int A[]);
  266.  
  267.   This function will take TotQuan and spread it as evenly as possible
  268.   over the array A.  Example:
  269.  
  270.     If you pass 13 in for TotQuan and 5 in for ArrayLen, A will have
  271.     the values 3, 2, 3, 2, 3.
  272.  
  273. 3.18      SystemTick()
  274.  
  275.   Prototype:  Long SystemTick()
  276.  
  277.   This function will return the current system tick.  On a PC
  278.   compatible machine (running any OS) you will get about 18 ticks per
  279.   second.
  280.  
  281. 3.19      CRC()
  282.  
  283.   Prototypes:  Word CRC(void* P,Long Size)
  284.                Word CRC(const Char* S)
  285.  
  286.   The functions will return a 16 bit CRC (commonly known as a CRC16)
  287.   based on the data given.  For the first function, pass a pointer to
  288.   anywhere in memory and then the number of bytes you want to be
  289.   included in the CRC calculation.  The second function performs a CRC
  290.   on a null terminated string (does not include the null terminator).
  291.  
  292.   A CRC is like an advanced checksum.  It is used for integrity
  293.   checking and encryption.  For more info, see the book
  294.   "C Programmer's Guide to Serial Communications" pp 533 - 550.
  295.  
  296. 4 Loop handling
  297.  
  298.   These loop handling macros are enhanced forms of existing loop
  299.   handling constructs.
  300.  
  301. 4.1 Loop
  302.  
  303.   Sets up an infinite loop.  Generally, stopping the loop is with a
  304.   "return".
  305.  
  306.     // program to keep on beeping until computer is reset
  307.     Loop
  308.       {
  309.         Beep();
  310.       }
  311.  
  312. 4.2 For
  313.  
  314.   This loop construct replaces the most common use of "for" in C
  315.   programming:  A variable starts at zero, is incremented with each
  316.   iteration and stops just before reaching the stop value. This macro
  317.   is a simpler, more readable, less error prone loop construct.
  318.   Replace
  319.  
  320.     int I;
  321.     for(I=0;I<15;I++)
  322.         Beep();
  323.  
  324.   with
  325.  
  326.     int I;
  327.     For(I,15)
  328.         Beep();
  329.  
  330.   Note that in the "for" version, "I" is referenced three times, there
  331.   are two semicolon delimiters, an assignment operator with an
  332.   initialization value, a comparison operator, an increment operator,
  333.   a stop value and is 17 characters long.  In the "For" version there
  334.   is one comma delimiter, one reference to "I", the stop value and is
  335.   9 characters long.  Whenever there's a bug in a piece of code that
  336.   has a "for" loop like the one above, the first thing I check is how
  337.   many iterations there were.  What would go through my mind in the
  338.   above example is:  "All three parts have an 'I', not a 'l', 'i' or '1'.
  339.   The comparison operator is '<' not '<=' or '>'.  'I' is initialized
  340.   to '0' not 'O' or '1'.  'I' is incremented properly.  Semicolons
  341.   were used, not commas or colons."  Now that I've used the "For"
  342.   macro for a while, I'm used to seeing the above example and thinking
  343.   "It did iterate 15 times."  There's only two things to check:  The
  344.   proper variable was used and the right stop value was used.
  345.  
  346.